package com.clouddong.exterapi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.clouddong.exterapi.service.ProbeTaskServiceI;
import com.clouddong.exterapi.util.JsonPluginsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@WebServlet({"/ProbeDataServlet"})
public class ProbeDataServlet extends HttpServlet
{
	@Override
	public void init() throws ServletException
	{
		//spring的方法解决servlet注入service的问题
		SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());
	}
	@Autowired
	public ProbeTaskServiceI proServiceI;
  private static final long serialVersionUID = 5638669728137623133L;
  
  private final static Logger logger=LoggerFactory.getLogger(ProbeDataServlet.class);

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    try
    {
    	 response.setCharacterEncoding("UTF-8");  
    	 String uploadPath = getServletContext().getRealPath("/");//获取文件路径  
    	 //ProbeTaskServiceI probeTaskServiceI=new ProbeTaskServiceImpl();
    	 //下载文件到指定目录
		 proServiceI.probeData(request, uploadPath);
    	 //probeData(request,uploadPath);  
    	 java.util.Map<String, String> retMap=new HashMap();
    	 retMap.put("status_code", "0");
    	 retMap.put("description", "Success!");
    	 JSONObject retJson=JsonPluginsUtil.beanToJsonObj(retMap);
    	 logger.info(retJson.toString());
    	 response.getWriter().println(retJson);  

    }
    catch (Exception e) {
    	java.util.Map<String, String> retMap=new HashMap();
   	 	retMap.put("status_code", "1");
   	 	retMap.put("description", "error!");
   	 	JSONObject retJson=JsonPluginsUtil.beanToJsonObj(retMap);
   	 	response.getWriter().println(retJson); 
   	 	e.printStackTrace();
    }
  }
  
}